<?php

class Application_Model_Users extends Common_Model {

    const ROLA_ADMIN = 99;
    const ROLA_GUEST = 0;

    public $tableName = 'Users';

    /**
     * 
     * @return \Zend_Paginator_Adapter_DbSelect
     */
    public function getAdapterPagin($where = null, $order = null) {

        $select = $this->db->select()->from('v_users');

        if ($where != null)
            $select->where($where);
        if ($order != null)
            $select->order($order);

        $adapter = new Zend_Paginator_Adapter_DbSelect($select);


        return $adapter;
    }

    public function getForm() {
        $form = parent::getForm();

        $form->setOptions(array(
            'style' => 'width:350px;margin:0px auto',
        ));

        $active = new Zend_Form_Element_Checkbox('active', array(
                    'label' => 'Aktywny'
                ));
        $name = new Zend_Form_Element_Text('name', array(
                    'label' => 'Nazwa',
                    'size' => '50',
                ));
        $email = new Zend_Form_Element_Text('email', array(
                    'label' => 'Email',
                    'size' => '50',
                ));
        $email->setRequired(true);

        $password = new Zend_Form_Element_Password('password', array(
                    'label' => 'Hasło'
                ));
        $password->addValidator('identical', false, array('repassword', 'Podaj takie samo hasło'));
        $password->setRequired(true);

        $repassword = new Zend_Form_Element_Password('repassword', array(
                    'label' => 'Powtórz'
                ));

        $login = new Zend_Form_Element_Text('login', array(
                    'label' => 'Login',
                    'disabled' => 'disabled',
                ));
        $login
                ->setRequired(true)
                ->addValidator('notEmpty')
                ->addValidator('stringLength', false, array(3, 16));


        $form->addDisplayGroup(array(
            $login, $active, $name, $email, $password, $repassword
                ), 'gr-main');

        return $form;
    }

    public function buildWhereFromFiltered($params) {
        $p = array();

        foreach ($params as $name => $param) {
            if ($param == -1)
                continue;
            switch ($name) {
                case "id";
                    $p[] = 'userId = ' . $param . '';
                    break;
                case "createdod":
                    $p[] = 'created >= "' . $param . '"';
                    break;
                case "createddo":
                    $p[] = 'created <= "' . $param . ' 23:59:59"';
                    break;
                case "lastod":
                    $p[] = 'lastLogin >= "' . $param . '"';
                    break;
                case "lastdo":
                    $p[] = 'lastLogin <= "' . $param . ' 23:59:59"';
                    break;
                case "items":
                    $p[] = 'items = ' . $param . '';
                    break;
                case "status":
                    $p[] = 'active = ' . $param . '';
                    break;
                case "userId":
                    $p[] = 'userId = ' . $param . '';
                    break;
            }
        }

        return count($p) > 0 ? '' . implode(' and ', $p) . '' : null;
    }
    
    public function getByEmail($email){
         return $this->table->fetchRow('email like"' . $email.'"');
    }

    public function buildOrderFromFiltered($params) {
        $p = array();

        foreach ($params as $name => $param) {
            if ($param == -1)
                continue;
            switch ($name) {
                case "id";
                    $p[] = 'userId  ' . $param . '';
                    break;
                case "login":
                    $p[] = 'login ' . $param . '';
                    break;
                case "items":
                    $p[] = 'items ' . $param . '';
                    break;
                case "created":
                    $p[] = 'created ' . $param . '';
                    break;
                case "lastLogin":
                    $p[] = 'lastLogin ' . $param . '';
                    break;
                case "status":
                    $p[] = 'active ' . $param . '';
                    break;
            }
        }

        return count($p) > 0 ? $p : null;
    }

}

